import { reactive } from 'vue';
import { Window, LogicalPosition } from '@tauri-apps/api/window';

const currentWindow = Window.getCurrent();
export function useDragDrop() {
    const state = reactive({
        isDragging: false,
        currentX: 0,
        currentY: 0,
        initX: 0,
        initY: 0
    });

    const startDrag = (e) => {
        state.isDragging = true;
        state.initX = e.clientX - e.clientX;
        state.initY = e.clientY - e.clientY;
        state.currentX = e.clientX;
        state.currentY = e.clientY;
    };

    const doDrag = (e) => {
        if (!state.isDragging) return;
        e.preventDefault();
        requestAnimationFrame(async () => {
            const posX = e.screenX - state.initX - state.currentX;
            const posY = e.screenY - state.initY - state.currentY;
            // 更新窗口实际位置
            await currentWindow.setPosition(new LogicalPosition(posX, posY));
        });
    };

    const endDrag = () => {
        state.isDragging = false;
    };

    return {
        ...state,
        startDrag,
        doDrag,
        endDrag
    }
}

